<?php
/**
 * Created By base
 * Author: Virus
 * Date: 2020/3/31
 * Time: 21:38
 */

namespace app\admin\logic;


use app\common\model\mysql\Config;

class ConfigLogic
{
    protected $model = null;

    public function __construct()
    {
        $this->model = new Config();
    }

    public function getLists($where)
    {
        $result = $this->model->getListsOfConfig($where);
        if (empty($result)) {
            return [];
        }

        return $result->toArray();
    }

    /**
     * @param $data
     * @return bool
     * @throws \think\Exception
     */
    public function addConfig($data)
    {
        $result = $this->model->addData($data);
        if (!$result) {
            throw new \think\Exception("添加失败");
        }

        return true;
    }

    /**
     * @param $config_id
     * @param $data
     * @return bool
     * @throws \think\Exception
     */
    public function saveConfig($config_id, $data)
    {
        $res = $this->model->saveConfig($config_id, $data);
        if (false === $res) {
            throw new \think\Exception("配置编辑失败");
        }

        return true;
    }

    /**
     * @param $id
     * @return array|mixed
     */
    public function getConfigOneById($id)
    {
        $result = $this->model->getOneDataById($id);
        if (empty($result)) {
            return [];
        }

        return $result;
    }

    /**
     * @param $id
     * @param $data
     * @return bool
     * @throws \think\Exception
     */
    public function updateConfig($id, $data)
    {
        $result = $this->model->updateDataById($id, $data);
        if (false === $result) {
            throw new \think\Exception("配置组编辑失败");
        }

        return true;
    }

    /**
     * @param $id
     * @return mixed
     * @throws \think\Exception
     */
    public function deleteById($id)
    {
        $result = $this->model->deleteOneData($id);
        if (!$result) {
            throw new \think\Exception("配置删除失败");
        }

        return $result;
    }
}